package org.osforce.platform.dao.jpa;

import java.util.List;

import javax.persistence.Query;

import org.osforce.platform.dao.IUserDao;
import org.osforce.platform.entity.User;
import org.osforce.platform.vo.Page;
import org.springframework.stereotype.Repository;

/**
 * 
 * @author gavin
 * 
 */
@Repository("userDao")
public class UserDao extends AbstractJpaDao<User> implements IUserDao{

	private static final String findAll="SELECT u FROM User AS u";
	private static final String deleteById="DELETE FROM User u WHERE u.id = :id";
	private static final String countAll="SELECT count(*) FROM User";
	private static final String findByUserName="SELECT u FROM User AS u WHERE u.userName = :userName";

	public UserDao(){
		super(User.class);
	}

	public void deleteById(Object id){
		Query query=getEntityManager().createQuery(deleteById);
		query.setParameter("id",id);
		query.executeUpdate();
	}

	public User findByUserName(String userName){
		Query query=getEntityManager().createQuery(findByUserName);
		query.setParameter("userName",userName);
		return (User)query.getSingleResult();
	}

	@SuppressWarnings("unchecked")
	public List<User> findAll(){
		Query query=getEntityManager().createQuery(findAll);
		return query.getResultList();
	}

	public Page<User> findAllByPage(Page<User> page){
		Query findQuery=getEntityManager().createQuery(findAll);
		Query countQuery=getEntityManager().createQuery(countAll);
		return super.findByPage(page,countQuery,findQuery);
	}

}
